/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hcbank.bussiness;

import com.hcbank.classes.Account;
import com.hcbank.classes.Customer;
import com.hcbank.jpa.controllers.AccountEntityJpaController;
import com.hcbank.jpa.controllers.CustomerEntityJpaController;
import com.hcbank.jpa.entities.AccountEntity;
import com.hcbank.jpa.entities.CustomerEntity;
import com.hcbank.jpa.entities.helper.AccountHelper;
import com.hcbank.jpa.entities.helper.CustomerHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author NHAHV
 */
public class CustomerController {

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("HCBankPU");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    CustomerEntityJpaController customerJpaController = new CustomerEntityJpaController(null, entityManagerFactory);
    AccountEntityJpaController accountJpaController = new AccountEntityJpaController(null, entityManagerFactory);
    AccountHelper accountHelper = new AccountHelper();
    CustomerHelper customerHelper = new CustomerHelper();
  

    public List<Account> GetCustomerAccounts(Integer CustomerId) {
        List<Account> list = new ArrayList<>();
        try {

            CustomerEntityJpaController customerEntityJpaController = new CustomerEntityJpaController(null, entityManagerFactory);
            CustomerEntity findCustomerEntity = customerEntityJpaController.findCustomerEntity(CustomerId);
            List<AccountEntity> accountEntityList = findCustomerEntity.getAccountEntityList();
            for (AccountEntity accountEntity : accountEntityList) {
                list.add(accountHelper.toAccount(accountEntity));
            }
        } catch (Exception e) {
          java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            return list;
        }
    }
    
    public Customer GetCustomerById(Integer customerId){
        CustomerEntity findCustomerEntity = customerJpaController.findCustomerEntity(customerId);
        return customerHelper.toCustomer(findCustomerEntity);
    }


}
